package rules

dialect "java"
import java.util.List
import cn.doitedu.eagle.beans.RuleResult
import java.util.ArrayList
import java.util.HashMap
import cn.doitedu.eagle.beans.RuleParam
import cn.doitedu.eagle.beans.DroolRuleParam
import cn.doitedu.eagle.beans.RuleParamEvent
import cn.doitedu.eagle.queryservice.UserActionCountQueryService
import cn.doitedu.eagle.queryservice.UserActionCountQueryService


// 规则名称
rule triggerTest1
    when
        //触发事件为 addcart
        $e:DroolRuleParam(trigEventId == "addCart")
        // 触发次数限制：1
        $r:RuleParam(trigCount < 1) from $e
        //$e:DroolRuleParam()
        //$r:RuleParam() from $e
        //$p:RuleEventParam(eventId == "addCart") from $r
        //$m:HashMap("productId" == "765")

    then
          // 用户基本属性及阈值设置,如：
          //  - vip等级=vip-3
          //  - 性别=female
          // HashMap<String,String> userProperties = new HashMap<>();
          // userProperties.put("vip","=,vip-3");
          // userProperties.put("gender","=,female");
          // $e.setUserProperties(userProperties);


          // 用户画像标签及阈值设置,如：
          //  - 用户总消费额>8000,
          //  - 用户最近月签到总次数=80,
          //  - 用户优惠券总使用额>500
          // HashMap<String,String> userProfileProperties = new HashMap<>();
          // userProfileProperties.put("cost_total",">,8000");
          // userProfileProperties.put("sign_count_month","=,80");
          // userProfileProperties.put("coupon_user_totalamt",">,500");
          // $e.setUserProfileProperties(userProfileProperties);

          // 设置触发次数限制
          $e.getRuleParam().setTrigLimit(1);

          // 行为次数条件及阈值设置,如：
          // - collect(收藏)行为在指定时段发生过2次
          HashMap<String,String> props1 = new HashMap<>();
          props1.put("adId", "14");
          RuleEventParam ruleEvent1 = new RuleEventParam("adShow", props1, "2021-03-10 00:00:00", "-1", 2);
          // - share(分享)行为在指定时段发生过2次
          HashMap<String,String> props2 = new HashMap<>();
          //props2.put("search", "857");
          RuleEventParam ruleEvent2 = new RuleEventParam("search", props2, "2021-03-10 00:00:00", "-1", 2);
          // - 将上面2个时间条件加入list
          ArrayList<RuleEventParam> actionRules = new ArrayList<>();
          actionRules.add(ruleEvent1);
          actionRules.add(ruleEvent2);

          // 把事件条件list，加入到“规则整体"参数对象中
          $e.getRuleParam().setActionJudgeEvents(actionRules);
          $e.getRuleParam().setRuleId("triggerTest1" +
           "");

          // 获取“行为属性类阈值条件查询对象“
          UserActionCountQueryService userActionCountQueryServiceImpl = $e.getUserActionCountQueryService();

          // 行为序列条件,如：
          // - 指定时间段内，依次发生过 addcart、adshow行为
          // ArrayList<String> eventSeq = new ArrayList<>();
          // eventSeq.add("addcart");
          // eventSeq.add("collect");
          // $e.setEventSeq(eventSeq);

          // 查询数据，判断是否满足规则阈值，插入最终结果
          $e.getRuleParam().setHit(
             userActionCountQueryServiceImpl.queryByRangeAndMinEventCounts($e.getRuleParam())
          );

    end